package com.hspedu.homework;

import java.util.Scanner;

public class Homework02 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("====请输入相关信息====");
        System.out.print("用户名：");
        String userName= scan.next();
        try {
            boolean b1 = m1(userName);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
            System.out.print("请重新输入用户名：");
            userName= scan.next();
        }

        System.out.print("密码：");
        String password = scan.next();
        try {
            boolean b2 = m2(password);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
            System.out.print("请重新输入密码：");
            password= scan.next();
        }

        System.out.print("邮箱：");
        String email = scan.next();
        try {
            boolean b3 = m3(email);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
            System.out.print("请重新输入邮箱：");
            email= scan.next();
        }
    }

    public static boolean m1(String userName) {
        int n1 = userName.length();// 用户名长度

        if (!(n1 >= 2 && n1 <= 4 )) {
            throw new RuntimeException("用户名输入不正确，要求用户名长度为 2 到 4");
        }

        return true;
    }
    public static boolean m2(String password) {
        int n2 = password.length();// 密码长度
        boolean b1 = true;// 密码是否全为数字
        for (int i = 0; i < password.length(); i++) {
            if (!(password.charAt(i) >= '0' && password.charAt(i) <= '9')) {
                b1 = false;
            }
        }

        if (!(n2 == 6 && b1)) {
            throw new RuntimeException("密码输入不正确，要求密码长度为 6，并且全为数字");
        }

        return true;
    }
    public static boolean m3(String email) {
        boolean b2 = true;// 邮箱是否正确
        int pos1 = -1, pos2 = -1;
        for (int i = 0; i < email.length(); i++) {
            if (email.charAt(i) == '@') pos1 = i;
            if (email.charAt(i) == '.') pos2 = i;
        }
        if (pos1 == -1 || pos2 == -1 || pos2 < pos1) b2 = false;

        if (!b2) {
            throw new RuntimeException("邮箱输入不正确，要求邮箱必须要有 @ 和 . 并且 @ 在 . 前面");
        }

        return true;
    }
}
